

/*
additional regressions of inflation swaps on auction shocks

output:
- f2d_infl_swaps*.pdf: coeffs across inflation swap years

*/

* plotting options
set scheme lean_uncluttered


************************************************************************
************************************************************************
* inflation swap regressions

* merge daily Datastream shocks
use ../data/auction_notesbonds_govpx_shock.dta, clear
rename auctionDate date
* note: for daily data, only keep later auction if multiple occur in one day
* only a handful of multiple auctions
bys date (close_time): gen _idx_keep = _n==_N
keep if _idx_keep
drop _idx_keep

* daily Datastream data
merge m:1 date using ../data/input/datastream_daily_shocks.dta, ///
	keep(master match) keepusing(D_PICAIS*) nogen

***************************
n di "infl swap regressions; all terms"
* for newey-west SEs
sort release_time
gen _T = _n
tsset _T

* rescale to basis/log points
ds D_PICAIS*
foreach var of varlist `r(varlist)' {
	replace `var' = 100*`var'
}


* all 
matrix res = J(30,3,.)
matrix res_iv = J(30,3,.)
local infl_swap_years 1 2 3 4 5 6 7 8 9 10 12 15 20 25 30
foreach y of local infl_swap_years {
	if `y'<10 local yy "`y'Y"
	else local yy "`y'"
	
	* store regression results in matrix
	* OLS
	ivreg2 D_PICAIS`yy' D_shock, r bw(9)
	scalar b = _b[D_shock]
	scalar se = _se[D_shock]
	scalar cil = b - 2*se
	scalar ciu = b + 2*se
	matrix res[`y',1] = b, cil, ciu
	
	* IV
	ivreg2 D_PICAIS`yy' (D_shock = D_b2c_all), r bw(9)
	scalar b = _b[D_shock]
	scalar se = _se[D_shock]
	scalar cil = b - 2*se
	scalar ciu = b + 2*se
	matrix res_iv[`y',1] = b, cil, ciu
}

* to dataset
preserve
* OLS
clear
svmat res
rename res1 res_coef
rename res2 res_cil
rename res3 res_ciu
gen tau = _n

* plot
* note: need ylabel format to fix weird spacing
twoway line res_coef res_cil res_ciu tau, ///
    legend(off) ylabel(, format( %9.1f) ) ///
    lcolor("scheme p1" "scheme p1" "scheme p1") ///
	lpattern(solid shortdash shortdash ) ///
	xtitle("Inflation swap rate, years") ytitle("D{sub:t} Coeff.") ///
    nodraw

* display with correct size and export
local fname f2d_infl_swaps_all
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


* IV
clear
svmat res_iv
rename res_iv1 res_coef
rename res_iv2 res_cil
rename res_iv3 res_ciu
gen tau = _n

* plot
twoway line res_coef res_cil res_ciu tau, ///
    legend(off) ylabel(, format( %9.1f) ) ///
    lcolor("scheme p1" "scheme p1" "scheme p1") ///
	lpattern(solid shortdash shortdash ) ///
	xtitle("Inflation swap rate, years") ytitle("D{sub:t} Coeff.") ///
    xsize(8) ysize(6)

* display with correct size and export
local fname f2d_iv_infl_swaps_all
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace

restore


***************************
n di "infl swap regressions; by terms"

local infl_swap_years 1 2 3 4 5 6 7 8 9 10 12 15 20 25 30
levelsof term_group, local(mats)
foreach m of local mats {
	n di "`m'"
	matrix res_`m' = J(30,3,.)
	matrix res_iv_`m' = J(30,3,.)
	foreach y of local infl_swap_years {
		if `y'<10 local yy "`y'Y"
		else local yy "`y'"
		
		* store regression results in matrix
		* OLS
		ivreg2 D_PICAIS`yy' D_shock if term_group==`m', r bw(9)
		scalar b = _b[D_shock]
		scalar se = _se[D_shock]
		scalar cil = b - 2*se
		scalar ciu = b + 2*se
		matrix res_`m'[`y',1] = b, cil, ciu
		
		* IV
		ivreg2 D_PICAIS`yy' (D_shock = D_b2c_all) if term_group==`m', r bw(9)
		scalar b = _b[D_shock]
		scalar se = _se[D_shock]
		scalar cil = b - 2*se
		scalar ciu = b + 2*se
		matrix res_iv_`m'[`y',1] = b, cil, ciu

	}

	* to dataset
	preserve
	* only label bottom x-axis
	if `m'==10 | `m'==30 {
		local x_axis_str xtitle("Inflation swap rate, years")
	}
	else local x_axis_str xtitle("")
	local y_axis_str ytitle("D{sub:t}{sup:(`m'Y)} Coeff.")
	
	* OLS
	clear
	svmat res_`m'
	rename res_`m'1 res_coef
	rename res_`m'2 res_cil
	rename res_`m'3 res_ciu
	gen tau = _n
	* plot
	twoway line res_coef res_cil res_ciu tau, ///
		legend(off) `x_axis_str' `y_axis_str' ///
		lcolor("scheme p1" "scheme p1" "scheme p1") ///
		lpattern(solid shortdash shortdash ) ///
		title("`m'-Year") name("infl_swap_mat`m'", replace) nodraw
	
	* IV
	clear
	svmat res_iv_`m'
	rename res_iv_`m'1 res_coef
	rename res_iv_`m'2 res_cil
	rename res_iv_`m'3 res_ciu
	gen tau = _n
	* plot
	twoway line res_coef res_cil res_ciu tau, ///
		legend(off) `x_axis_str' `y_axis_str' ///
		lcolor("scheme p1" "scheme p1" "scheme p1") ///
		lpattern(solid shortdash shortdash ) ///
		title("`m'-Year") name("iv_infl_swap_mat`m'", replace) nodraw
	
	restore
}
* combine with correct size and export
* OLS
local fname f2d_infl_swaps_by_term
graph combine infl_swap_mat2 infl_swap_mat3 ///
	infl_swap_mat5 infl_swap_mat7 infl_swap_mat10 infl_swap_mat30, ///
	rows(3) imargin(tiny) xsize(5) ysize(7) xcommon name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


* IV
local fname f2d_iv_infl_swaps_by_term
graph combine iv_infl_swap_mat2 iv_infl_swap_mat3 ///
	iv_infl_swap_mat5 iv_infl_swap_mat7 iv_infl_swap_mat10 iv_infl_swap_mat30, ///
	rows(3) imargin(tiny) xsize(5) ysize(7) xcommon name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace




